﻿#include "../../libcat/include/cat-module.hpp"
#include "../../libcat/include/util/json.h"
#include "../../libcat/include/util/jsoncpp.cpp"

namespace {

	static HWND catHwnd = NULL;

	static int Width = 0, Height = 0;
	static long ret = 0; 
	static bool mouse_right_button = false;
	static bool mouse_left_button = true;
	static  sf::Clock textTimer;
	static bool desktop_pet = false;
	static bool T_keystate = false;			//T键状态值和鼠标穿透状态值
	static bool is_mouse_through = false;		//鼠标穿透状态值
	static bool text_drawing = false;		//是否显示文本，主要用于防止在程序启动时就显示文本
	static bool is_pass = false;
	static bool is_last_pass = false;

	static void drawText(cat_window* cwindow) {
		if (textTimer.getElapsedTime() < sf::seconds(2) && text_drawing)
		{
			sf::Text textForLock; 
			textForLock.setPosition(0, 60);
			textForLock.setFont(*cwindow->catfont);
			textForLock.setFillColor(sf::Color(255, 126, 126));
			textForLock.setCharacterSize(50);
			if (is_mouse_through)
				textForLock.setString(L"鼠标穿透已开启");
			else
				textForLock.setString(L"鼠标穿透已关闭");
			cwindow->window->draw(textForLock);
		}
		else if (sf::seconds(2) < textTimer.getElapsedTime() && textTimer.getElapsedTime() < sf::milliseconds(2500) && text_drawing)
		{
			sf::Text textForLock; 
			textForLock.setPosition(0, 60);
			textForLock.setFont(*cwindow->catfont);
			textForLock.setFillColor(sf::Color(255, 126, 126, 255 * ((sf::milliseconds(2500) - textTimer.getElapsedTime()) / sf::milliseconds(500))));
			textForLock.setCharacterSize(50);
			if (is_mouse_through)
				textForLock.setString(L"鼠标穿透已开启");
			else
				textForLock.setString(L"鼠标穿透已关闭");
			cwindow->window->draw(textForLock);
		}

	};

	static void lockCat(cat_window* cwindow)
	{
		//判断是否按下了组合键ctrl:17 shift:16 T:84   并判断是否为桌宠状态。  使用GetKeyState()函数是为了在穿透状态下也能接受热键
		if (is_pass!= is_last_pass)
		{
			//判断是否刚按下T
			if (T_keystate == false)
			{
				//设置是否鼠标穿透，并重置文本显示的计时器
				if (is_mouse_through) {
					SetWindowLong(catHwnd, GWL_EXSTYLE, ret);
					is_mouse_through = false;
					textTimer.restart();
					is_last_pass = is_pass;
				}
				else
				{					
					SetWindowLong(catHwnd, GWL_EXSTYLE, ret | WS_EX_LAYERED | WS_EX_TRANSPARENT);
					is_mouse_through = true;
					textTimer.restart();
					is_last_pass = is_pass;
				}
			}
			//设置T键状态， 在第一次按下组合键时才开启文本显示
			T_keystate = true;
			text_drawing = true;
		}
		//松开键盘后清除T键状态
		else
			T_keystate = false;
	}

}

const char* get_name() {
	return u8"鼠标穿透插件";
}

static void* OnMouseClick(const cat_mouse_click_event* event, bool mouse_up) {
	switch (event->_button)
	{
	case MOUSE_LEFT:
		if (mouse_up)
			mouse_left_button = false;
		else
			mouse_left_button = true;
		break;
	case MOUSE_RIGHT:
		if (mouse_up)
			mouse_right_button = false;
		else
			mouse_right_button = true;
	default:
		break;
	}
	return NULL;
};

void* Init(cat_data_t* settings) {
	Width = settings->json["main_decoration"]["window_size"][0].asInt();
	Height = settings->json["main_decoration"]["window_size"][1].asInt();
	is_pass = settings->json["main_decoration"]["is_mouse_through"].asBool();

	ret = GetWindowLong(catHwnd, GWL_EXSTYLE);
	return settings;
}

void* Update(cat_data_t* settings) {

	Width = settings->json["main_decoration"]["window_size"][0].asInt();
	Height = settings->json["main_decoration"]["window_size"][1].asInt();
	is_pass = settings->json["main_decoration"]["is_mouse_through"].asBool();
	return settings;
}

void* Release() {
	catHwnd = NULL;
	T_keystate = false;
	is_mouse_through = false;	
	text_drawing = false;

	return NULL;
};

void* Run(void* _window) {

	if (!catHwnd)
		catHwnd = cat_get_window_handle((cat_window*)_window);

	cat_window* cwindow = (cat_window*)_window;

	lockCat(cwindow);
	drawText(cwindow);		

	return _window;
};

CAT_DECLARE_MODULE()
bool cat_module_load (){
	cat_source_info pet {};
	pet.id = -1;
	pet.type = CAT_SOURCE_TYPE_UI;
	pet.Init = Init;
	pet.Update = Update;
	pet.Release = Release;
	pet.Run = Run;
	pet.OnMouseClick = OnMouseClick;
	pet.get_name = get_name;

	cat_register_source(&pet);
	return true;
}
